(function () {
    function QueryData() {
        $.post('/dashboard/city/computed', function (d) {
            let today = d.data.today;
            let yesterday = d.data.yesterday;
            let sites = today.flatMap(e => e.sites);
            sites.sort((a1, a2) => a1.passedTotal === a2.passedTotal ? 0 : a1.passedTotal > a2.passedTotal ? 1 : -1);
            console.dir({sites: JSON.parse(JSON.stringify(sites))});
            let splice = sites.splice(0, 7);
            charts1['11'](splice.map(e => e.siteName), splice.map(e => e.passedTotal));
            charts1['12'](today.map(e => e.regionName),
                today.map(e => e.passedTotal),
                today.map(e => e.passedOverFloatWeightTotal));

            let withPerCity = today.map(e => {
                if (!e.passedTotal) {
                    e.per = 0;
                } else {
                    e.per = (e.passedOverFloatWeightTotal / e.passedTotal).toFixed(3) - 0;
                }
                return e;
            })

            withPerCity = withPerCity.sort((a1, a2) => a1.per === a2.per ? 0 : a1.per > a2.per ? -1 : 1).splice(0, 4).map(e => {
                return {name: e.regionName, value: !e.passedOverFloatWeightTotal ? 0 : e.passedOverFloatWeightTotal, per: Number((e.per * 100).toFixed(0))}
            })

            let length = 4 - withPerCity.length;
            if (length !== 0) {
                for (let i = 0; i < length; i++) {
                    withPerCity.push({name: '', value: 0, per: 0})
                }
            }
            charts1['13'](withPerCity);

            charts1['21']({
                datetime: new Date().toLocaleString(), today: today.reduce((a1, a2) => {
                    return a1 + a2.passedOverFloatWeightTotal
                }, 0),
                yesterday: yesterday
            }, today);

        })
    }

    let lastRecordId = null;
    let lastRecordDatetime = null;
    function QueryRecords() {
        $.post('/dashboard/records',{date: lastRecordDatetime, id: lastRecordId}, function (d) {
            let tableDomNumber = $('#table_body').children().length;
            if (!(d.data instanceof Array)) {
                return;
            }
            if(d.data[0]) {
                lastRecordDatetime = d.data[0].time;
                lastRecordId = d.data[0].id;
            }
            if (tableDomNumber === 0) {
                let length = d.data.length || 0;
                for (let i = 0; i < length; i++) {
                    $('#table_body').append(buildRecord(d.data[i]));
                }
            } else {
                let length = d.data.length || 0;
                if (tableDomNumber + length > 200) {
                    let number = tableDomNumber + length - 200;
                    for (let i = number; i < 200; i++) {
                        let number1 = 200 - number;
                        $('#table_body').children()[number1].remove();
                    }
                }
                for (let i = length - 1; i >= 0; i--) {
                    $('#table_body').prepend(buildRecord(d.data[i]));
                }
            }

        });
    }
    function QuerySystem() {
        $.post('/dashboard/system_info', function (d) {
            let data = d.data;
            charts1['32'](data, data);
        })
    }

    let imgs = [{},{},{},{}];
    function QueryImgs() {
        $.post('/dashboard/imgs', function (d) {
            let data = d.data;
            console.log(data);
            if (!(data instanceof Array)) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                imgs[i] = {name: data[i].vehicleNo, src: (data[i].imgs).split(",")[2]}
                $('.img_ img')[i].src = 'http://192.168.1.97:9090' + imgs[i].src;
                $('.img_ span')[i].innerText = imgs[i].name;
            }
        })
    }

    let axleName = ['一轴', '二轴', '三轴', '四轴', '五轴', '六轴', '七轴', '八轴'];
    function QueryAxle() {
        $.post('/dashboard/number_of_axle', function (d) {
            let data = d.data;
            data = data.map(e => {
                return {value: e.c, name: axleName[e.axle_sum - 1]};
            })
            charts1['33'](data);
        })
    }

    function buildRecord({time, siteName, vehicleNo, weight}) {
        return $("<div class='row'></div>")
            .append($("<span></span>").text(time),
                $("<span></span>").text(siteName),
                $("<span></span>").text(vehicleNo.substring(0,8)),
                $("<span></span>").text(weight)
            )


    }


    QueryData();
    QueryRecords();
    QuerySystem();
    QueryAxle();
    QueryImgs();

    setInterval(QueryData, 20 * 1000);
    setInterval(QueryRecords, 20 * 1000);
    setInterval(QuerySystem, 20 * 1000);
    setInterval(QueryImgs, 60 * 1000);
    setInterval(QueryAxle, 60 * 1000);

    // charts1['12'](["城市A", "城市B", "城市C", "城市D", "城市E", "城市F", "城市G"],
    //     [120, 200, 150, 80, 70, 110, 130],
    //     [123, 123, 123, 123, 123, 123, 123]);
    // charts1['13']([{name: '地区一', value: 1000,per: 50},
    //     {name: '地区二', value: 2000,per: 50},
    //     {name: '地区三', value: 4000,per: 40},
    //     {name: '地区四', value: 1000,per: 5},]);
    // charts1['21']({datetime: new Date().toLocaleString(), today: 28313,yesterday: 23010});
    // charts1['32']({cpu: 80, ram: 20, rom: 20}, {ramRem: '8.1G', ramUsed: '7.6G', romRem: '200G', romUsed: '100G'});
    // charts1['33']([{
    //     value: 1240,
    //     name: '一轴'
    // }, {value: 15098, name: '二轴'}, {value: 4098, name: '三轴'}, {value: 2656, name: '四轴'}]);

})();